package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.DrugPlanModel;

public class DrugAddListAdpater extends BaseAdapter {

	private List<DrugPlanModel> data;
	private LayoutInflater inflater;
	private Context context;
	private boolean isDelete;

	public DrugAddListAdpater(List<DrugPlanModel> data, Context context,
			boolean isDelete) {
		super();
		this.data = data;
		this.context = context;
		this.isDelete = isDelete;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub

		arg1 = this.inflater.inflate(R.layout.item_drug_add_item, null);
		TextView tv_drug_list_drugname = (TextView) arg1
				.findViewById(R.id.tv_drug_list_drugname);
		TextView tv_drug_list_breakfast = (TextView) arg1
				.findViewById(R.id.tv_drug_list_breakfast);
		TextView tv_drug_list_drugfrequency = (TextView) arg1
				.findViewById(R.id.tv_drug_list_drugfrequency);
		TextView tv_drug_list_drugdose = (TextView) arg1
				.findViewById(R.id.tv_drug_list_drugdose);

		ImageView iv_drug_list_delete = (ImageView) arg1
				.findViewById(R.id.iv_drug_list_delete);

		DrugPlanModel drug = data.get(arg0);
		tv_drug_list_drugname.setText(drug.getDrugName());
		tv_drug_list_breakfast.setText(drug.getBreakfast());
		tv_drug_list_drugfrequency.setText(drug.getDrugfrequency());
		tv_drug_list_drugdose.setText(drug.getDrugdose());

		if (isDelete) {
			iv_drug_list_delete.setVisibility(View.VISIBLE);
		} else {
			iv_drug_list_delete.setVisibility(View.GONE);

		}
		iv_drug_list_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				data.remove(arg0);
				notifyDataSetChanged();
			}
		});

		return arg1;
	}
}
